package com.yctan.base.config;


import ch.qos.logback.core.util.FileUtil;
import com.yctan.base.exception.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.BindException;

/**
 * 全局异常处理
 * @author sec
 * @create 2020-06-19 16:49
 **/
@RestControllerAdvice
public class MyExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 参数校验异常
     * @param bindException
     * @return
     */
    @ExceptionHandler(BindException.class)
    public ApiErrorResponse bindException(BindException bindException) {
        String message = bindException.getMessage();
        ApiErrorResponse apiErrorResponse = new ApiErrorResponse(message);
        if (logger.isDebugEnabled()) {
            logger.warn(message, bindException);
        }
        else {
            logger.warn(message);
        }
        return  apiErrorResponse;
    }

    /**
     * 接口异常
     * @param apiException
     * @return
     */
    @ExceptionHandler(ApiException.class)
    public ApiErrorResponse apiException(ApiException apiException) {
        String message = apiException.getMessage();
        ApiErrorResponse apiErrorResponse = new ApiErrorResponse(message);
        if (logger.isDebugEnabled()) {
            logger.warn(message, apiException);
        }
        else {
            logger.warn(message);
        }
        return  apiErrorResponse;
    }


    /**
     * 对第三方金融机构接口异常
     * @param openApiException
     * @return
     */
    @ExceptionHandler(OpenApiException.class)
    public OpenApiErrorResponse openApiException(OpenApiException openApiException) {
        String data = openApiException.getData();
        String message = openApiException.getMessage();
        OpenApiErrorResponse openApiErrorResponse = new OpenApiErrorResponse(data);
        if (logger.isDebugEnabled()) {
            logger.warn(message, openApiException);
        }
        else {
            logger.warn(message);
        }
        return  openApiErrorResponse;
    }

    /**
     * token校验异常
     * @param tokenException
     * @return
     */
    @ExceptionHandler(TokenException.class)
    public ApiErrorResponse tokenException(TokenException tokenException) {
        String message = tokenException.getMessage();
        ApiErrorResponse apiErrorResponse = new ApiErrorResponse(FinalCode.ACCOUNT_EXPIRED, message);
        if (logger.isDebugEnabled()) {
            logger.warn(message, tokenException);
        }
        else {
            logger.warn(message);
        }
        return apiErrorResponse;
    }

    /**
     * 用户异常
     * @param userException
     * @return
     */
    @ExceptionHandler(UserException.class)
    public ApiErrorResponse userException(UserException userException) {
        String message = userException.getMessage();
        ApiErrorResponse apiErrorResponse = new ApiErrorResponse(FinalCode.ACCOUNT_INVALID, message);
        if (logger.isDebugEnabled()) {
            logger.warn(message, userException);
        }
        else {
            logger.warn(message);
        }
        return apiErrorResponse;
    }

    /**
     * 请求异常
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ApiErrorResponse httpMessageNotReadableException(HttpMessageNotReadableException e) {
        String message="错误原因：1.没有请求体；2.请求体中的参数类型不匹配";
        ApiErrorResponse apiErrorResponse;
        if (logger.isDebugEnabled()) {
            apiErrorResponse = new ApiErrorResponse(message+" ("+e.getMessage()+")");
            logger.warn(message, e);
        }
        else {
            apiErrorResponse = new ApiErrorResponse(message);
            logger.warn(message);
        }
        return  apiErrorResponse;
    }

    /**
     * 其他异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ApiErrorResponse exception(Exception e) {
        ApiErrorResponse apiErrorResponse;
        logger.error("注意: 出现此错误，后端开发应该修改代码处理此异常，处理抛出ApiException或者其自定义的子类");
        if (logger.isDebugEnabled()) {
            String message="注意: 出现此错误，后端开发应该修改代码处理此异常，处理抛出ApiException或者其自定义的子类"+" ("+e.getMessage()+")";
            apiErrorResponse = new ApiErrorResponse(message);
            logger.error(e.getMessage(), e);
        }
        else {
            String message="服务运行出错了,请联系服务提供商解决此问题";
            apiErrorResponse = new ApiErrorResponse(message);
            logger.error(e.getMessage(), e);
        }
        return apiErrorResponse;
    }
}
